import { defineStore } from "pinia"
import { Categroy, Image, ImageShare } from '../type/entity'
function sortImages(images: Image[]){
  images.sort((img1, img2) => img2.createTime - img1.createTime)
}

export const useImageStore = defineStore('image', {
  // 推荐使用 完整类型推断的箭头函数
  state: () => {
    return {
      images: [] as Image[],
      operatingImage: {} as Image,
      operating: false,
      categoryCreating: false,
      categories: [] as Categroy[],
      previewingImage: {} as Image,
      previewShow: false,
      shareOperating: false,
      categorySharing: {} as Categroy,
      categorySelectValue: '',
      imageShares: [] as ImageShare[],
      debounceResizeImage: {} as () => void
    }
  },
  actions: {
    setImages(newImages: Image[]){
      sortImages(newImages)
      this.images = newImages
    },
    addImage(img: Image){
      this.images.push(img)
      sortImages(this.images)
    },
    removeImage(id:number){
      this.images = this.images.filter(val => val.id != id)
    }
  }
})